﻿using Photo.Application.Core.Dict.Dto;
using Photo.Application.Core.Service.Dict;

namespace Photo.Application.Core.Dict
{
    /// <summary>
    /// 数据字典
    /// </summary>
    [ApiDescriptionSettings(ApiGroupConsts.Core, Module = ApiGroupConsts.Core)]
    [AllowAnonymous]
    public class DictService : IDynamicApiController
    {
        private readonly IRepository<SysDictEntity> _sysDictRepository;
        private readonly DictCoreService _dictCoreService;
        public DictService(IRepository<SysDictEntity> sysDictRepository, DictCoreService dictCoreService)
        {
            _sysDictRepository = sysDictRepository;
            _dictCoreService = dictCoreService;
        }

        /// <summary>
        /// 获取下拉框数据字典键值对列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<List<XmSelectTreeDto>> GetTreeList(DictInput input)
        {
            var list = await _sysDictRepository.Query()
                .Where(o => o.Type == input.Type)
                .WhereIF(!string.IsNullOrWhiteSpace(input.PId), o => o.PId == input.PId)
                .Where(o => o.IsActive == true)
                .OrderBy(o => o.Sort)
                .Select(o => new XmSelectTreeDto
                {
                    Value = o.Id,
                    Name = o.Value,
                    ParentValue = o.PId
                })
                .ToListAsync();

            return list.ToTree("Value", "ParentValue", "Children", "");
        }

    }
}
